def processCat(token,catdict):
        toreturn = [0 for i in range(len(catdict))];
        toreturn[catdict.index(token)] = 1;
        return toreturn;


workclass=['Private', 'Self-emp-not-inc', 'Self-emp-inc', 'Federal-gov', 'Local-gov', 'State-gov', 'Without-pay', 'Never-worked','?'];
education=['Bachelors', 'Some-college', '11th', 'HS-grad', 'Prof-school', 'Assoc-acdm', 'Assoc-voc', '9th', '7th-8th', '12th', 'Masters', '1st-4th', '10th', 'Doctorate', '5th-6th', 'Preschool','?'];
maritalstatus = ['Married-civ-spouse', 'Divorced', 'Never-married', 'Separated', 'Widowed', 'Married-spouse-absent', 'Married-AF-spouse','?'];
occupation = ['Tech-support', 'Craft-repair', 'Other-service', 'Sales', 'Exec-managerial', 'Prof-specialty', 'Handlers-cleaners', 'Machine-op-inspct', 'Adm-clerical', 'Farming-fishing', 'Transport-moving', 'Priv-house-serv', 'Protective-serv', 'Armed-Forces','?'];
relationship= ['Wife', 'Own-child', 'Husband', 'Not-in-family', 'Other-relative', 'Unmarried','?'];
race = ['White', 'Asian-Pac-Islander', 'Amer-Indian-Eskimo', 'Other', 'Black','?']
sex = ['Female','Male','?']
nativecountry = ['United-States', 'Cambodia', 'England', 'Puerto-Rico', 'Canada', 'Germany', 'Outlying-US(Guam-USVI-etc)', 'India', 'Japan', 'Greece', 'South', 'China', 'Cuba', 'Iran', 'Honduras', 'Philippines', 'Italy', 'Poland', 'Jamaica', 'Vietnam', 'Mexico', 'Portugal', 'Ireland', 'France', 'Dominican-Republic', 'Laos', 'Ecuador', 'Taiwan', 'Haiti', 'Columbia', 'Hungary', 'Guatemala', 'Nicaragua', 'Scotland', 'Thailand', 'Yugoslavia', 'El-Salvador', 'Trinadad&Tobago', 'Peru', 'Hong', 'Holand-Netherlands','?']
label = ['>50K.','<=50K.']
data = [];

with open('../data/adult.test') as f:
	line=f.readline();
	index = 0
	while len(line.strip()) > 0:
		index +=1
		currentrow = [];
		tokens = line.split(',');
		tokens = map(lambda t: t.strip(),tokens)
		currentrow.append('1')
		currentrow.append(tokens[0])
		w=processCat(tokens[1],workclass)
		currentrow.extend(w)
		e=processCat(tokens[3],education);
		currentrow.extend(e)
		edunum = tokens[4]
		currentrow.append(edunum);
		currentrow.extend(processCat(tokens[5],maritalstatus))
		currentrow.extend(processCat(tokens[6],occupation))
		currentrow.extend(processCat(tokens[7],relationship))
		currentrow.extend(processCat(tokens[8],race))
		currentrow.extend(processCat(tokens[9],sex))
		currentrow.append(tokens[10]) #captial-gain
		currentrow.append(tokens[11]) #capital-loss
		currentrow.append(tokens[12]) # hours-per-week
		currentrow.extend(processCat(tokens[13],nativecountry))
		currentrow.append(label.index(tokens[14]))
		currentrow = map(lambda c: str(c),currentrow)
		line = f.readline()
		data.append(currentrow)

data = map(lambda line: ','.join(line),data);
data = '\n'.join(data);

with open('../data/adult.test.processed','w') as f:
	f.write(data);
